package hig.no.group;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.SimpleCursorAdapter;
import android.widget.AdapterView.AdapterContextMenuInfo;

/*public class ListSettings extends ListActivity {

	private DBAdapter mDBHelper;
	private Cursor mC;

	/** Called when the activity is first created. */
    /*@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);
   
        mDBHelper = new DBAdapter(this);
        mDBHelper = mDBHelper.open();
     
        refreshList();

    }
	
	
	private void refreshList() {
		
		mC = mDBHelper.GetAllGPSlocations();
		startManagingCursor(mC);
		
		SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,R.layout.row, mC, new String[] {DBAdapter.COL_LATITUDE, DBAdapter.COL_LONGITUDE/*, DBAdapter.COL_STREET}, new int[] {R.id.txtText1, R.id.txtText2/*, R.id.txtText3});
		setListAdapter(adapter);
	}

	
	
}*/

public class ListSettings extends ListActivity {
    private static final int REQUEST_ADD = 1;
	private static final int REQUEST_EDIT = 2;
	private DBAdapter mDBHelper;
	private Cursor mC;

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);
        Full_Screen();
   
        mDBHelper = new DBAdapter(this);
        mDBHelper = mDBHelper.open();
     
        refreshList();

        registerForContextMenu(getListView());  //use context menu
    }

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.mymenu, menu);
		return true;
	}
	

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		
		mDBHelper.close();
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.contextmenu, menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId())
		{
		case R.id.miAdd:
			//Toast.makeText(this, "Add", Toast.LENGTH_SHORT).show();
			Intent i = new Intent();
			i.setClass(this, SettingsActivity.class);
			i.putExtra("id", 0);
			
			startActivityForResult(i, REQUEST_ADD);			
			
			return true;
		case R.id.miClear:
			mDBHelper.deleteAllBookmark();
			refreshList();
			
			return true;
		case R.id.miHome:
			//Toast.makeText(this, "Add", Toast.LENGTH_SHORT).show();
			//Intent j = new Intent();
			//j.setClass(this, MainActivity.class);
			//startActivity(j);			
			
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}



	@Override
	public boolean onContextItemSelected(MenuItem item) {
		
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
		int position = info.position;
		long id = info.id;
		
		
		switch (item.getItemId())
		{
		case R.id.miDelete:
			mDBHelper.deleteBookmark(id);			
			refreshList();
			
			return true;
		case R.id.miEdit:
			
			/*Cursor c = mC;
			c.moveToPosition(position);
			
			Intent i = new Intent();
			i.setClass(this, Input.class);
			
			i.putExtra(DBAdapter.COL_ID, id);
			
			i.putExtra(DBAdapter.COL_TITLE, c.getString(c.getColumnIndexOrThrow(DBAdapter.COL_TITLE)));
			i.putExtra(DBAdapter.COL_URL, c.getString(c.getColumnIndexOrThrow(DBAdapter.COL_URL)));
			
			startActivityForResult(i, REQUEST_EDIT);	*/	
			
			
			return true;
		default:
			return super.onContextItemSelected(item);
		}
	}
	
	private void refreshList() {
		
		mC = mDBHelper.GetAllBookmarks();
		startManagingCursor(mC);
		
		SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,R.layout.row, mC, new String[] {DBAdapter.COL_TITLE, DBAdapter.COL_URL}, new int[] {R.id.txtText1, R.id.txtText2});
		setListAdapter(adapter);
	}


	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		
		switch(requestCode)
		{
		case REQUEST_ADD:
			String url = data.getStringExtra("url");
			String title = data.getStringExtra("title");
			
			mDBHelper.createBookmark(title, url);

			refreshList();
			
			break;
			
		case REQUEST_EDIT:
			
			String editUrl = data.getStringExtra("url");
			String editTitle = data.getStringExtra("title");
			long updateId = data.getLongExtra("id", 0);
			
			mDBHelper.updateBookmark(updateId, editTitle, editUrl);
			
			refreshList();
			
			break;
		default:
			super.onActivityResult(requestCode, resultCode, data);
		}		
	}	
	
	
	public void Full_Screen(){
		requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
	}
}
